package com.zhaonongzi.tms.util;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.ArrayUtils;

import java.util.*;

/**
 * Created by wang'ao on 2016/9/29 0029.
 */
public class EChartConstructUtils {
    /**
     * echart:x数据
     * 从db中查询到的数据,并过滤重复的字段,为x轴的数据
     * @return
     */
    public static List<String> getXList(List<Map> dbData,String field) {
        List<String> s = new ArrayList<>() ;
        for (Map map : dbData) {
            String occurTime = MapUtils.getString(map,field);
            if(!s.contains(occurTime)) s.add(occurTime);
        }
        return s;
    }

    /**
     * chart:legends
     * @param dbData db数据
     * @param nickname 用来做legends的字段
     * @param distinguishField 作为legends的重复识别
     * @return
     */
    public static List<String> getLegends(List<Map> dbData, String nickname, String distinguishField) {
        List<String> s = new ArrayList<>() ;
        List<String> l = new ArrayList<>() ;
        for (Map map : dbData) {
            String fieldUnique = MapUtils.getString(map,distinguishField);
            if(!s.contains(fieldUnique)) {
                s.add(fieldUnique);
                l.add(MapUtils.getString(map,nickname));
            }
        }
        return l;
    }
    /**
     * chart:series序列
     * @param dbData db数据
     * @return
     */
    public static List<Map> getSeries(List<Map> dbData,List<String> xList,String legendsKeyField,String xlistFieldName,String legendFieldName,String chartType,String valueFieldName) {
        List<Map> r = new ArrayList<>() ;//返回的

        //整理数据{id:map{map{field:"",val:"",...}},...}
        /**
         * legendsKeyField:"occur_time"
         * xlistFieldName:"张三"
         * legendFieldName:"nickname"
         * valueFieldName:"avg_cost"
         */
        /**
         * {
         *  "001":{
         *      "2016-08-08":{
         *          "name":"张三",
         *          "value":"76.92"
         *      },
         *      "2016-08-09":{
         *          "name":"张三",
         *          "value":"71.91"
         *      },
         *      ...
         *
         *  },
         *  "007":{
         *      "2016-08-03":{
         *          "name":"李四",
         *          "value":"16.92"
         *      },
         *      "2016-08-05":{
         *          "name":"李四",
         *          "value":"21.91"
         *      },
         *      ...
         *
         *  },
         *  ...
         * }
         */
        Map<String,Map> identity = new HashMap<>() ;
        for (Map map : dbData) {
            //没有,创建
            if (!identity.containsKey(MapUtils.getString(map,legendsKeyField))) {
                Map<String,String> innerVal = new HashMap<>() ;
                innerVal.put("name",MapUtils.getString(map,legendFieldName)) ;
                innerVal.put("value",MapUtils.getString(map,valueFieldName)) ;

                Map<String,Map> inner = new HashMap<>() ;
                inner.put(MapUtils.getString(map,xlistFieldName),innerVal) ;

                identity.put(MapUtils.getString(map,legendsKeyField),inner) ;
            }
            //有,取出
            else {
                Map<String,String> innerVal = new HashMap<>() ;
                innerVal.put("name",MapUtils.getString(map,legendFieldName)) ;
                innerVal.put("value",MapUtils.getString(map,valueFieldName)) ;

                Map<String,Map> inner = MapUtils.getMap(identity,MapUtils.getString(map,legendsKeyField)) ;
                inner.put(MapUtils.getString(map,xlistFieldName),innerVal) ;
            }
        }
        Iterator<Map.Entry<String, Map>> iterator = identity.entrySet().iterator();
        while(iterator.hasNext()) {
            Map.Entry<String, Map> next = iterator.next();
            String [] data = new String[]{} ;
            String name = null ;

            for (String s : xList) {
                //第一级map
                Map valueInner = next.getValue();
                if (!valueInner.containsKey(s)) {
                    //此处放入空值,而不是0
                    data = ((String[]) ArrayUtils.add(data, null)) ;
                }
                else {
                    //第二级map
                    Map valueInnerInner = MapUtils.getMap(valueInner, s);
                    data = ((String[]) ArrayUtils.add(data, valueInnerInner.get("value"))) ;
                    name = MapUtils.getString(valueInnerInner,"name") ;
                }
            }

            Map inner = new HashedMap() ;
            inner.put("name",name) ;
            inner.put("data",data) ;
            inner.put("type",chartType) ;
            r.add(inner) ;
        }
        return r;
    }
}
